package com.yunduo.config;

import com.yunduo.common.YunDuoConst.OrderQueueParam;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq配置类
 *  订单超时处理
 */
@Configuration
public class RabbitmqConfig {
    //订单超时队列
    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-message-ttl", 1800000); //设置过期时间30分钟
        return new Queue(OrderQueueParam.ORDER_DELAY_QUEUE.getValue(), true, false, false, args);
    }

    //订单超时交换机
    @Bean
    public CustomExchange orderDelayExchange(){
        Map<String, Object> arguments = new HashMap<>(1);
        arguments.put("x-delayed-type", "direct"); //使用延迟队列插件
        return new CustomExchange(OrderQueueParam.ORDER_DELAY_CHANGE.getValue(),"x-delayed-message",true,false,arguments);
    }

    //绑定交换机
    @Bean
    public Binding orderDelayBinding(){
        return BindingBuilder.bind(orderDelayQueue()).to(orderDelayExchange()).with(OrderQueueParam.ORDER_DELAY_ROUTING_KEY.getValue()).noargs();
    }
}
